package com.example.wilson.myapplication;

import android.app.Dialog;

import android.support.v7.app.AlertDialog;
import android.support.v7.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.app.DatePickerDialog;
import android.widget.Toast;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    private TextView showData;
    private Button setdate;
    private int year;
    private int month;
    private  int day;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //第一个作业alertdialog
        /*Builder dialog = new AlertDialog.Builder(MainActivity.this);
        dialog.setIcon(android.R.drawable.ic_dialog_info);
        dialog.setTitle("welcome to android worlid");
        dialog.setMessage("欢迎使用wilsonJ的程序");
        dialog.setPositiveButton("yes", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                //确定之后
            }
        });
        dialog.setNegativeButton("no", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                //否定之后
            }
        });
        dialog.create();
        dialog.show();*/

        showData = (TextView) this.findViewById(R.id.showtime);
        setdate = (Button) this.findViewById(R.id.setdate);
        //初始化Calendar
        Calendar c = Calendar.getInstance(Locale.CHINA);
        Date mydate = new Date();

        c.setTime(mydate);

        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);
        //显示当前年月日
        showData.setText("当前日期：" + year + "-" + (month + 1) + "-" + day);

        //添加单击事件
        setdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DatePickerDialog my_dataPickerDialog = new DatePickerDialog(MainActivity.this, Datelistener, year, month, day);
                my_dataPickerDialog.show();

            }
        });
    }

    private DatePickerDialog.OnDateSetListener Datelistener = new DatePickerDialog.OnDateSetListener() {


        @Override
        public void onDateSet(DatePicker datePicker, int y, int m, int d) {

            Date current_date = new Date(year,month,day);
            Date select_date = new Date(y,m,d);

            if(select_date.after(current_date)){
                Toast.makeText(MainActivity.this,"设置日期无效",Toast.LENGTH_SHORT).show();
            }else{
                year = y;
                month = m;
                day = d;
                updateDate();
           }
        }

        private void updateDate(){
            showData.setText("当前日期：" + year + "-" + (month + 1) + "-" + day);
        }
    };



}
